Imports Iou = System.IO
Imports Reg = System.Text.RegularExpressions
Imports Sys = System
Imports Tex = System.Text

Namespace Enhancements.Core.Logging
    ''' <summary>A logging writer implementation which targets the console window.</summary>
    Public NotInheritable Class ConsoleWriter
        Inherits Writer

        ''' <summary>Initializes a ConsoleWriter instance.</summary>
        ''' <param name="_configuration">Configuration to use by the instance.</param>
        ''' <param name="_formatter">Formatter to use by the instance.</param>
        ''' <param name="_use">Use to use by the instance.</param>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        ''' <exception cref="IsNullException">_configuration is null, or _formatter is null.</exception>
        ''' <exception cref="IsOfTypeNotException">_configuration is not of type ConsoleWriterConfiguration.</exception>
        Public Sub New(ByVal _configuration As WriterConfiguration, ByVal _formatter As Formatter, ByVal _use As System.Boolean)
            Configuration = _configuration
            Formatter = _formatter
            Use = _use
        End Sub

        ''' <summary>Gets the configuration used by the instance.</summary>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        Public Overrides Property Configuration() As WriterConfiguration
            Get
                ___Assert.___IsDisposedNot("ConsoleWriter[instance]", Me.___DisposeCalled, True)

                Return MyBase.Configuration
            End Get
            Protected Friend Set(ByVal _value As WriterConfiguration)
                ___Assert.___IsDisposedNot("ConsoleWriter[instance]", Me.___DisposeCalled, True)
                ___Assert.___IsNullNot("Configuration._value", _value, True)
                ___Assert.___IsOfType("Configuration._value", _value, GetType(ConsoleWriterConfiguration), True)

                MyBase.Configuration = _value
            End Set
        End Property

        ''' <summary>Gets the formatter used by the instance.</summary>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        Public Overrides Property Formatter() As Formatter
            Get
                ___Assert.___IsDisposedNot("ConsoleWriter[instance]", Me.___DisposeCalled, True)

                Return MyBase.Formatter
            End Get
            Protected Friend Set(ByVal _value As Formatter)
                ___Assert.___IsDisposedNot("ConsoleWriter[instance]", Me.___DisposeCalled, True)
                ___Assert.___IsNullNot("Formatter._value", _value, True)

                MyBase.Formatter = _value
            End Set
        End Property

        ''' <summary>Gets and sets the value that defines whether the instance should be considered enabled for logging operations.</summary>
        ''' <exception cref="IsDisposedException">The instance has been disposed.</exception>
        Public Overrides Property Use() As System.Boolean
            Get
                ___Assert.___IsDisposedNot("ConsoleWriter[instance]", Me.___DisposeCalled, True)

                Return __Use
            End Get
            Set(ByVal _value As System.Boolean)
                ___Assert.___IsDisposedNot("ConsoleWriter[instance]", Me.___DisposeCalled, True)

                __Use = _value
            End Set
        End Property

        Friend Overrides Sub ___Write(ByVal _record As ___Record)
            ___Assert.___IsDisposedNot("ConsoleWriter[instance]", Me.___DisposeCalled, True)
            ___Assert.___IsNullNot("_record", _record, True)
            ___Assert.___IsEmptyNot("_record.___UnformattedIdentifier", _record.___UnformattedIdentifier, True)
            ___Assert.___IsNullNot("_record.___UnformattedCallingProcedure", _record.___UnformattedCallingProcedure, True)
            ___Assert.___IsNullNot("_record.___UnformattedObjects", _record.___UnformattedObjects, True)

            If ___Assert.___IsTrue("Use", Use, False) Then
                __Format(_record)
                __Write(_record)
            End If
        End Sub

        Private Sub __Format(ByVal _record As ___Record)
            Formatter.___Format(_record)
            ___Assert.___IsNullNot("_record.___Formatted", _record.___Formatted, True)
        End Sub

        Private Sub __Write(ByVal _record As ___Record)
            Sys.Console.WriteLine(CType(Configuration, ConsoleWriterConfiguration).Encoding.GetString(_record.___Formatted))
        End Sub

        Friend Overrides Sub ___Dispose()
            ___Assert.___IsDisposedNot("ConsoleWriter[instance]", Me.___DisposeCalled, True)
            ___DisposeCalled = True
        End Sub

    End Class
End Namespace
